const fetch = require('../../utils/common')
let app = getApp();
Page({
    data: {
        header:{},
        userInfo:{},
        userheaderImageList:[
            {headerImage:'../../image/installheaderimg.jpg',userName:'李老板',cnt:8},
            {headerImage:'../../image/installheaderimg.jpg',userName:'张老板',cnt:5},
            {headerImage:'../../image/installheaderimg.jpg',userName:'程老板',cnt:8},
            {headerImage:'../../image/installheaderimg.jpg',userName:'张老板',cnt:5},
        ],
        showList:[],
        toTop:false,
        selectType:'0',
        index:0,
        item:'全国',
        province:'全国',
        city:'全国',
        searchMode:'',
        showListTuiJian:[],
        showLogin:false,
        pageNum:1,
        current:1,
        pagesize:20,
        isShowAdd:0,
        totalPage:1,
        moreText:'加载更多...'


        
    },
    //
    turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=2'
      })
    },
    //上拉加载更多
    onReachBottom:function(){
        console.log('加载更多')
        this.setData({
          current:this.data.current+1
        })
        if (this.data.current<=this.data.totalPage) {
          this.getAllSheBei();
        }else{
          this.setData({
            moreText:'没有更多了'
          })
        }
        
    },
    showLoading:function () {
      console.log('123231')
      this.setData({
        showLogin:true
      })
    },
     onCannel:function(){
     this.setData({
        showLogin:false
      })
    },
    onLoginSuccess:function () {
      this.setData({
        showLogin:false
      })
    },
    onShow:function(){

       var chooseProvince= wx.getStorageSync('chooseProvince2');
       var chooseCity2=wx.getStorageSync('chooseCity2');
       console.log(chooseCity2)
       console.log(this.data.city)
       if (chooseCity2==this.data.city) {
        return
       }
      
        if (chooseProvince) {
          this.setData({
            city:wx.getStorageSync('chooseCity2')||'全国',
            item:wx.getStorageSync('chooseCity2')||'全国',
            province:wx.getStorageSync('chooseProvince2')||'全国'
          })
        }
      this.setData({
        showList:[]
      })
      this.getAllSheBei();
    },
    onLoad:function(){
      var _self=this;
       var address= wx.getStorageSync('address');
       var chooseProvince= wx.getStorageSync('chooseProvince2');
       if (!chooseProvince) {
          wx.setStorageSync('chooseProvince2',address.province||'全国');
          wx.setStorageSync('chooseCity2',address.city||'全国');
       }
        this.setData({
            address:address,
            city:address.city,
            item:address.city,
            province:address.province
        })

      getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header,
        showList:[],
        current:1
       })  

       this.getAllSheBei();
       // this.getAllSheBeiForTuiJian(address.province);
     })
    },
    onPullDownRefresh:function () {
      console.log('刷新')
      this.setData({
        showList:[],
        current:1,
        pagesize:20
      })
       this.getAllSheBei();


    },
    onPageScroll:function(e){
    console.log(e.scrollTop);//{scrollTop:99}
       if (e.scrollTop>500) {
        this.setData({
          toTop:true
        })
       }else{
          this.setData({
          toTop:false
        })
       }
    },
   
    getAllSheBei:function(){
      var url
      var province=this.data.province;
      var city=this.data.city;
      var type=this.data.selectType;
      console.log('type='+type)

      if (type==10||type==20) {
         url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20&type='+type+'&current='+this.data.current+'&pagesize='+this.data.pagesize+'&title='+this.data.searchMode||''
      }else if(type==30){
        url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20&current='+this.data.current+'&pagesize='+this.data.pagesize+'&isTestMachine=1'

      }else if(type==40||type==50||type==60){
        var num;
        if (type==40) {
           num=1
        }else if(type==50){
           num=2
        }
        else if(type==60){
            num=3
        }
         url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20&current='+this.data.current+'&pagesize='+this.data.pagesize+'&queryprice='+num
        
      }else if(type==70||type==80||type==90||type==100){
        console.log("********")
           var text;
          if (type==70) {
             text='中联'
          }else if(type==80){
             text='徐工'
          }
          else if(type==90){
              text='川建'
          }
          else if(type==100){
              text='大汉'
          }
          console.log("********")
      url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20&current='+this.data.current+'&pagesize='+this.data.pagesize+'&title='+text 
      }  
      else{
        url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20'+'&current='+this.data.current+'&pagesize='+this.data.pagesize+'&title='+this.data.searchMode||''
      }
      if (province!='全国') {
       url+='&province='+province+'&city='+city
      }
      
       var _self=this;
         wx.request({
            url: url,
            method:"GET",
            header: this.data.header,
            success: function(res) {
            
             if (res.data.code!=200) {
              wx.stopPullDownRefresh();
              return
             }
             _self.setData({
              showList:_self.data.showList.concat(res.data.obj.list),
              isShowAdd:res.data.iscity,
              totalPage:res.data.obj.pages
             })
              wx.stopPullDownRefresh();
           
         }
       })

    },
    onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1]
      })
      this.setData({
        showList:[]
      })
      this.getAllSheBei();
      this.getAllSheBeiForTuiJian(province.detail.split(',')[0]);

    },
    getUserInfo:function(){

      
    },
    moreEredarClick () { // 更多达人
        wx.navigateTo({
            url: '/pages/eredar/eredar'
        })
    },
    eredarClick () { // 达人详情
        wx.navigateTo({
            url: '/pages/eredarDetail/eredarDetail'
        })
    },
    turnComponyDetail:function(){
         wx.navigateTo({
            url: '/pages/componyDetail/componyDetail'
        })
    },
   
    turnPage:function(){
        wx.pageScrollTo({
           scrollTop: 1
        })
    },
   
    chooseType:function(e){
       let type=e.target.dataset.type;
       console.log(e.target.dataset.type)
       this.setData({
        selectType:e.target.dataset.type,
        showList:[],
        current:1
       })
        var _self=this;
       if (type=='0') {

       this.getAllSheBei();

       }else{
         this.getAllSheBei(type);
       }


       // else if(type=='10'){

       // this.getAllSheBei(10);
  
       // }
       // else if(type=='20'){

       // this.getAllSheBei(20);
        
       // }else if(type=='30'){

       // this.getAllSheBei(30);
        
       // }else if(type=='40'){

       // this.getAllSheBei(40);
        
       // }else if(type=='50'){

       // this.getAllSheBei(50);
        
       // }else if(type=='60'){

       // this.getAllSheBei(60);
        
       // }

    },
    bindPickerChange:function(e){
        this.setData({
          index: e.detail.value
        })
    },
    updateValue(e) {
       console.log(e.detail.value)
       this.setData({
         searchMode:e.detail.value
       })
   },
    search:function () {
      console.log(this.data.searchMode)
      var  url;
      var userInfo= wx.getStorageSync('userInfo');
      if(this.data.searchMode.length<1){
       url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?province='+this.data.province+'&city='+this.data.city+'&status=20&type='+this.data.selectType
      }else{
       url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?title='+this.data.searchMode+'&province='+this.data.province+'&city='+this.data.city+'&status=20&type='+this.data.selectType
      }
    
      
       var _self=this;
         wx.request({
            url: url,
            method:"GET",
            header: {
                'openid':userInfo.openid, //公用请求头
                'content-type': 'application/json'
            },
            success: function(res) {
             console.log(res)
             _self.setData({
              showList:res.data.obj.list
             })
            
           
         }
       })

    },
    getAll:function() {
       var userInfo= wx.getStorageSync('userInfo');
      var url;
      url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?current=1&pagesize=100&status=20'
    
       var _self=this;
         wx.request({
            url: url,
            method:"GET",
            header: {
                'openid':userInfo.openid, //公用请求头
                'content-type': 'application/json'
            },
            success: function(res) {
             console.log(res.data.obj.list)
             _self.setData({
              showListTuiJian:res.data.obj.list
             })
           
           
         }
       })

    },
    
    getAllSheBeiForTuiJian:function(province){
      
      var userInfo= wx.getStorageSync('userInfo');
      var url;
      url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?current=1&status=20&pagesize=100&province='+province
    
       var _self=this;
         wx.request({
            url: url,
            method:"GET",
            header: {
                'openid':userInfo.openid, //公用请求头
                'content-type': 'application/json'
            },
            success: function(res) {
             console.log(res.data.obj.list)
             if (res.data.obj.list.length==0) {
               _self.getAll();
               return
             }
             _self.setData({
              showListTuiJian:res.data.obj.list
             })
           
           
         }
       })

    },
    onShareAppMessage:function(){
         return {
              title: '真实一手严选机管家带看免押金',
              imageUrl: '' // 图片 URL
          }
    },
    onShareTimeline:function(){
         return {
              title: '真实一手严选机管家带看免押金',
              imageUrl: '' // 图片 URL
          }
    },

});